package com.program.facesigninsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.program.facesigninsystem.dao.*;
import com.program.facesigninsystem.entity.*;
import com.program.facesigninsystem.service.ISignInCourseService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class SignInCourseServiceImpl implements ISignInCourseService {
    @Autowired
    private ISignInCourseDao iSignInCourseDao;
    @Autowired
    private ISignInActivityDao iSignInActivityDao;
    @Autowired
    private ICourseOrganizationDao iCourseOrganizationDao;
    @Autowired
    private IUserInfoDao iUserInfoDao;
    @Autowired
    private ISuperiorsOrganizationDao iSuperiorsOrganizationDao;

    @Override
    public void addNewCourse(String ccid, String name, String week, String section, String uid, String createtime, String signinclass_id,String year,String semester) {
        iSignInCourseDao.addNewCourse(ccid, name, week, section, uid, createtime, signinclass_id,year,semester);
    }

    @Override
    public List<SignInCourse> selectbyuid(String uid, int pageSize, int pageNum) {
        PageHelper.startPage(pageSize, pageNum);
        return iSignInCourseDao.selectbyuid(uid);
    }

    @Override
    public List<SignInActivity> selectActbyccid(String ccid, int pageSize, int pageNum) {
        PageHelper.startPage(pageSize, pageNum);
        return iSignInActivityDao.selectActbyccid(ccid);
    }

    @Override
    public Boolean deleteCourse(String ccid) {
        try{
            iSignInCourseDao.deleteCouByccid(ccid);
            iCourseOrganizationDao.deletebyccid(ccid);
            return true;
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public Boolean applygroup(String oid, String ccid, String createtime) {
        try{
            if(iCourseOrganizationDao.selectapplyhistory(oid, ccid)==null){
                iCourseOrganizationDao.addNewCO(oid, ccid,1,createtime);
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public Boolean rejectapply(String coid) {
        try{
            iCourseOrganizationDao.updateapplystates3(coid);
            return true;
        }catch(Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public Boolean passapply(String coid) {
        try{
            iCourseOrganizationDao.updateapplystates2(coid);
            return true;
        }catch(Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public Boolean untiebind(String coid) {
        try{
            iCourseOrganizationDao.updateapplystates4(coid);
            return true;
        }catch(Exception e){
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public List<JSONObject> selectapplyhistory(String oid, int pageSize, int pageNum) {
//        PageHelper.startPage(pageSize, pageNum);
        List<CourseOrganization> courseOrganizationList=iCourseOrganizationDao.selectapplyhistory1(oid);
        List<JSONObject> jsonObjectList=new ArrayList<>();
        for(CourseOrganization courseOrganization:courseOrganizationList){
            SignInCourse signInCourse=iSignInCourseDao.selectbyccid(courseOrganization.getCcid());
            JSONObject activityObject = new JSONObject();
            activityObject.put("ccid",signInCourse.getCcid());
            activityObject.put("application_status",courseOrganization.getApplicationstatus());
            activityObject.put("name",signInCourse.getName());
            activityObject.put("week",signInCourse.getWeek());
            activityObject.put("section",signInCourse.getSection());
            activityObject.put("createtime",signInCourse.getCreatetime());
            activityObject.put("uname",iUserInfoDao.selectUser(signInCourse.getUid()).get("name"));
            activityObject.put("coid",courseOrganization.getId());
            jsonObjectList.add(activityObject);
        }
        return jsonObjectList;
    }
    @Override
    public List<JSONObject> selectbind(String oid, int pageSize, int pageNum) {
//        PageHelper.startPage(pageSize, pageNum);
        List<CourseOrganization> courseOrganizationList=iCourseOrganizationDao.selectbind(oid);
        List<JSONObject> jsonObjectList=new ArrayList<>();
        for(CourseOrganization courseOrganization:courseOrganizationList){
            SignInCourse signInCourse=iSignInCourseDao.selectbyccid(courseOrganization.getCcid());
            JSONObject activityObject = new JSONObject();
            activityObject.put("ccid",signInCourse.getCcid());
            activityObject.put("application_status",courseOrganization.getApplicationstatus());
            activityObject.put("name",signInCourse.getName());
            activityObject.put("week",signInCourse.getWeek());
            activityObject.put("section",signInCourse.getSection());
            activityObject.put("createtime",signInCourse.getCreatetime());
            activityObject.put("uname",iUserInfoDao.selectUser(signInCourse.getUid()).get("name"));
            activityObject.put("coid",courseOrganization.getId());
            jsonObjectList.add(activityObject);
        }
        return jsonObjectList;
    }

    @Override
    public List<JSONObject> selectbindallsubs(String oid, int pageSize, int pageNum) {
        Set<String> allOrgIds = new HashSet<>();
        getAllSubOrgIds(oid, allOrgIds); // 获取包括自己在内的所有下级组织

        List<CourseOrganization> allCourses = new ArrayList<>();
        for (String currentOid : allOrgIds) {
            List<CourseOrganization> temp = iCourseOrganizationDao.selectbind(currentOid);
            allCourses.addAll(temp);
        }

        // 手动分页
        int fromIndex = Math.min((pageNum - 1) * pageSize, allCourses.size());
        int toIndex = Math.min(fromIndex + pageSize, allCourses.size());
        List<CourseOrganization> pageList = allCourses.subList(fromIndex, toIndex);

        List<JSONObject> result = new ArrayList<>();
        for (CourseOrganization co : pageList) {
            SignInCourse course = iSignInCourseDao.selectbyccid(co.getCcid());
            JSONObject obj = new JSONObject();
            obj.put("ccid", course.getCcid());
            obj.put("application_status", co.getApplicationstatus());
            obj.put("name", course.getName());
            obj.put("week", course.getWeek());
            obj.put("section", course.getSection());
            obj.put("createtime", course.getCreatetime());
            obj.put("uname", iUserInfoDao.selectUser(course.getUid()).get("name"));
            obj.put("coid", co.getId());
            obj.put("oid", co.getOid()); // 加上所属组织,先放着，万一需要
            result.add(obj);
        }

        return result;
    }

    private void getAllSubOrgIds(String oid, Set<String> allOids) {
        if (allOids.contains(oid)) return;
        allOids.add(oid);
        List<SuperiorsOrganization> children = iSuperiorsOrganizationDao.selectbind(oid);
        for (SuperiorsOrganization child : children) {
            getAllSubOrgIds(child.getOid(), allOids);
        }
    }



    @Override
    public List<JSONObject> selectbindbycname(String oid, String name, int pageSize, int pageNum) {
        List<CourseOrganization> courseOrganizationList=iCourseOrganizationDao.selectbindbycname(oid, name);
        List<JSONObject> jsonObjectList=new ArrayList<>();
        for(CourseOrganization courseOrganization:courseOrganizationList){
            SignInCourse signInCourse=iSignInCourseDao.selectbyccid(courseOrganization.getCcid());
            JSONObject activityObject = new JSONObject();
            activityObject.put("ccid",signInCourse.getCcid());
            activityObject.put("application_status",courseOrganization.getApplicationstatus());
            activityObject.put("name",signInCourse.getName());
            activityObject.put("week",signInCourse.getWeek());
            activityObject.put("section",signInCourse.getSection());
            activityObject.put("createtime",signInCourse.getCreatetime());
            activityObject.put("uname",iUserInfoDao.selectUser(signInCourse.getUid()).get("name"));
            activityObject.put("coid",courseOrganization.getId());
            jsonObjectList.add(activityObject);
        }
        return jsonObjectList;
    }

    @Override
    public List<JSONObject> selectsup(String ccid, int pageSize, int pageNum) {
        List<CourseOrganization> courseOrganizationList=iCourseOrganizationDao.selectsup(ccid);
        List<JSONObject> jsonObjectList=new ArrayList<>();
        for(CourseOrganization courseOrganization:courseOrganizationList){
            SignInCourse signInCourse=iSignInCourseDao.selectbyccid(courseOrganization.getCcid());
            JSONObject activityObject = new JSONObject();
            activityObject.put("ccid",signInCourse.getCcid());
            activityObject.put("application_status",courseOrganization.getApplicationstatus());
            activityObject.put("name",signInCourse.getName());
            activityObject.put("week",signInCourse.getWeek());
            activityObject.put("section",signInCourse.getSection());
            activityObject.put("createtime",signInCourse.getCreatetime());
            activityObject.put("uname",iUserInfoDao.selectUser(signInCourse.getUid()).get("name"));
            activityObject.put("coid",courseOrganization.getId());
            jsonObjectList.add(activityObject);
        }
            return jsonObjectList;
    }

    @Override
    public List<String> selectlidbyccid(String ccid) {
        List<String> lidList = new ArrayList<>(); // 创建一个新的列表用于存储 lid
        List<SignInActivity> signInActivityList = iSignInActivityDao.selectActbyccid(ccid);
        for (SignInActivity signInActivity : signInActivityList) {
            List<String> lid = iSignInActivityDao.selectlid1ByActId(signInActivity.getActId());
            lidList.addAll(lid); // 将当前迭代获取到的 lid 添加到 lidList 中
        }
        return lidList; // 返回 lidList
    }

    @Override
    public List<String> selectlidbyactid(String actid) {
        List<String> lid=iSignInActivityDao.selectlid1ByActId(actid);
        return lid;
    }

    @Override
    public SignInCourse selectbyccid(String ccid) {
        return iSignInCourseDao.selectbyccid(ccid);
    }


}
